var WxAutoImage = require('../../js/wxAutoImageCal.js');
var WxParse = require('../wxParse/wxParse.js');
var app = getApp();
Page({
  data: {
    wid:'',
    currentTab:'',
    mc: '',//辅导班名称
    nj: '',//年级
    bj: '',//班级
    km: '',//科目
    jj:'',//辅导班简介
    fy:0,//辅导班的费用
    xe: 0,//报名人员限额
    bmrs: 0,//报名人数
    sksj: '',//上课时间
    skdd: '',//上课地点
    bmzlsm: '',//报名资料说明
    sfjzsj: '',//收费截止时间
    bmlb:'',

    pris:[],//价格列表
    teas: [],//教师列表

    priarr: [],//被选中的收费
    allprice:0 //总价格
  },
  cusImageLoad: function (e) 
  {
    var that = this;
    that.setData(WxAutoImage.wxAutoImageCal(e));
  },

  onLoad: function (options) {
    var that = this;   
    //获取传递的参数
    this.setData({
      wid: options.wid
    })  

    var that = this;
    var openid = wx.getStorageSync('openid');
    if (openid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      that.getdata();
    }
   
    /** 
    * 获取系统信息 
    */
   /*  wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    }); */
  },
  getdata: function () {//定义函数名称
      wx.showLoading({
        title: '加载中',
      })
      var that = this;   
    
      wx.request({
        url: app.myUlife.ulifeUrl +'/wechart/whk_list.html',//请求地址
        data: {//发送给后台的数据
          wid: that.data.wid
        },
        header: {//请求头
          "Content-Type": "applciation/json"
        },
        method: "GET",//get为默认方法/POST
        success: function (res) {
          that.setData({
            mc: res.data.MC,
            nj: res.data.NJ,
            bj: res.data.BJ,
            km: res.data.KM,
            jj: res.data.JJ,
            pris: res.data.pris,
            teas: res.data.teas,
            fy: res.data.FY,
            allprice: res.data.FY,
            xe: res.data.XE,//报名人员限额
            bmrs: res.data.BMRS,//报名人数
            sfjzsj: res.data.SFJZSJ,//收费截止时间
            sksj: res.data.SKSJ,//上课时间
            skdd: res.data.SKDD,//上课地点
            bmzlsm: res.data.BMZLSM,//报名资料说明
            bmlb: res.data.BMLB,
          });
          wx.hideLoading()
        },
        fail: function (err) { },//请求失败
        complete: function () { }//请求完成后执行的函数
      })
  },

  /** 
    * 点击tab切换 
    */
  swichNavSwi: function (e) {
    var that = this;
    var currentTab = e.target.dataset.current;
    if (this.data.currentTab === currentTab) {
      return false;
    } else {
      that.setData({
        currentTab: currentTab
      })
    }   
   
  },

  niming: function (e) { 
    var index = e.target.dataset.index;
    var value = e.detail.value;
  
    if (value.length!=0) {
      var nps={};
      var pris = this.data.pris;
      for (var i = 0; i < pris.length; i++) {
        if (pris[i].FID == value[0]) {
          nps = {
            index: index,
            FID: pris[i].FID,
            XMMC: pris[i].XMMC,
            FEE: pris[i].FEE,
          }
        }
      }
      this.data.priarr.push(nps);
    }else{
      var priSarr=[];
      var priarr = this.data.priarr;
      for (var i = 0; i < priarr.length;i++){
        if (priarr[i].index != index){
          priSarr.push(priarr[i]);
        }
      }
      this.setData({
        priarr: priSarr
      })
    }

    var allprice=this.data.fy;
    var priarr = this.data.priarr;
    for (var i = 0; i < priarr.length; i++) {
      allprice = parseFloat(allprice) + parseFloat(priarr[i].FEE);
    }
    this.setData({
      allprice: allprice
    })
  },
 
/** 
  * 购买
  */
  toPay: function (e) {
    if (this.data.xe == this.data.bmrs){
      wx.showModal({
        title: '友情提示',
        content: "对不起,报名人数已经满额了!",
        showCancel: !1,
      })
    }else{
      wx.navigateTo({
        url: '../../pages/confirmorder/confirmorder'//跳转到订单确认页面
      })
    }
  }

})
